//
//  MainViewController.swift
//  weibo
//
//  Created by 杨晨 on 16/3/8.
//  Copyright © 2016年 杨晨. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {
    
    //这个方法是tabBar中的加号按钮的点击事件
    func click() {
        print("我是加号按钮")
        
    }
    
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
       
        let mainTabBar = MainTabBar()
        
        mainTabBar.addBtn.addTarget(self, action: "click", forControlEvents: .TouchUpInside)
        addController();
//        public var tabBar: UITabBar { get } // Provided for -[UIActionSheet showFromTabBar:]. Attempting to modify the contents of the tab bar directly will throw an exception.
        //因为 tabBar为只读属性，所以不能直接改变，可以通过kvc来强行改变
        //实例化tabBar
        print(tabBar.classForCoder)
        let myTabBar = MainTabBar()
        //value是自己自定义的tabBar，key是系统的要被修改的tabBar
        setValue(myTabBar, forKey: "tabBar")
        print(tabBar.classForCoder)
        
        
}

    
    private func addController(){
        
        addChildViewControllers(HomeTableViewController(), title:"首页", imageName: "tabbar_home")
        addChildViewControllers(MessageTableViewController(), title:"消息",imageName:"tabbar_message_center")
        addChildViewControllers(DiscoverTableViewController(), title:"发现", imageName:"tabbar_discover")
        addChildViewControllers(ProfileTableViewController(), title:"我", imageName:"tabbar_profile")
        

    }
    
    
    private func addChildViewControllers(vc:UIViewController,title:String,imageName:String) {
        
       vc.tabBarItem.title = title
       vc.tabBarItem.image = UIImage(named: imageName)
       let nav=UINavigationController(rootViewController: vc)
       
        vc.navigationItem.title=title;
        //设置选中的图片的背景色
        vc.tabBarItem.selectedImage=UIImage(named: imageName + "_selected")?.imageWithRenderingMode(.AlwaysOriginal)
        //设置文字的颜色(第一种方法)
        tabBar.tintColor=UIColor.orangeColor()
        //设置文字的颜色第二种方法
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.orangeColor()], forState: .Selected)
        
       addChildViewController(nav)
       
       
    }
    
    //这个方法是tabBar中的加号按钮的点击事件
   
  
}
